from rknnlite.api import RKNNLite as RKNN
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import pickle



# model='linear_model.rknn'
# model='CNN_Lee20231201.rknn'
# model = 'LDA_Lee20240122.rknn'
model='LDA_rknn1.6_Lee20240126.rknn'


# input_test_data = [23, 32, 197, 1022, 1, 1, 18, 24, 169, 458, 1, 1, 25, 31, 173, 697, 1, 1, 27, 36, 195, 1221, 1, 1]
# 定义特征名称和数据
feature_names = ['AverageAbsoluteValue_Channel1', 'RootMeanSquare_Channel1', 'MeanFrequency_Channel1' \
    , 'Hjorth Activity_Channel1', 'Hjorth Mobility_Channel1', 'Hjorth Complexity_Channel1' \
    , 'AverageAbsoluteValue_Channel2', 'RootMeanSquare_Channel2', 'MeanFrequency_Channel2' \
    , 'Hjorth Activity_Channel2', 'Hjorth Mobility_Channel2', 'Hjorth Complexity_Channel2' \
    , 'AverageAbsoluteValue_Channel3', 'RootMeanSquare_Channel3', 'MeanFrequency_Channel3' \
    , 'Hjorth Activity_Channel3', 'Hjorth Mobility_Channel3', 'Hjorth Complexity_Channel3' \
    , 'AverageAbsoluteValue_Channel4', 'RootMeanSquare_Channel4', 'MeanFrequency_Channel4' \
    , 'Hjorth Activity_Channel4', 'Hjorth Mobility_Channel4', 'Hjorth Complexity_Channel4']
features_Sample = [23, 32, 197, 1022, 1, 1, 18, 24, 169, 458, 1, 1, 25, 31, 173, 697, 1, 1, 27, 36, 195, 1221, 1, 1]
features_MergedArray = np.array(features_Sample)
features_MergedArrayD2 = features_MergedArray.reshape(1, -1)
# 转换为DataFrame并设置特征名称
features_MergedDataFrame = pd.DataFrame(features_MergedArrayD2, columns=feature_names)

with open('scaler_PytorchCNNVersion.pkl', 'rb') as f:
    scaler = pickle.load(f)

features_scaled = scaler.transform(features_MergedDataFrame)
# 将 float64 转换为 float32
#features_scaled_float32 = features_scaled.astype(np.float32)
features_scaled_float32 = features_scaled.astype(np.float16)



# rknn=RKNN()
rknn=RKNN(verbose=True)

ret=rknn.load_rknn(path=model)
print('--> Init runtime environment')
ret = rknn.init_runtime(
    #target=None,
    #target="rk3588",
    # target_sub_class=None,
    device_id=None,
    # perf_debug=False,
    # eval_mem=False,
    async_mode=False,
    core_mask=RKNN.NPU_CORE_AUTO
    #core_mask=0
)

if ret != 0:
    print('Init runtime environment failed')
    exit(ret)
print('done')


print(22222222111111222222222)
# output=rknn.inference(inputs=[features_scaled_float32])
output=rknn.inference(inputs=[features_scaled_float32])
print(111111)
print(output)
print(111111)






